var $;
layui.config({
	base : "js/"
}).use(['form','layer','jquery'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;
    /**加载下拉列表**/
      getApartment();
      form.render(null,"apartSelect");
    form.on('select(apartSelect)', function(data){
	  console.log(data.elem); //得到select原始DOM对象
	  console.log(data.value); //得到被选中的值
	  console.log(data.othis); //得到美化后的DOM对象
	  var type ;
	  if($(data.elem).attr("name") == "apartSn")
	  {
	  	type = 1;
	  }else
	  {
	  	type = 2;
	  }
	  getChildrenDistrict(type);
	  form.render(null,"apartSelect");
	  
	});      
 	form.on("submit(addUser)",function(data){
 		var roomSn = data.field.apartSn+'-'+data.field.floorSn+'-'+data.field.roomSn;
	 	var dto = {
 			"studentName": $(".studentName").val(),
 			"studentSn": $(".studentSn").val(),
 			"studentSex": data.field.sex,
 			"studentAdder": $(".studentAdder").val(),
 			"studentMobile": $(".studentMobile").val(),
 			"auditStatus": "1",
 			"status": "1",
 			"roomSn": roomSn
 		};
        $.ajax({
	        url: API_URL_HEAD+"student/create",
	        type: 'post',
	        contentType: "application/json; charset=utf-8",
	        data: JSON.stringify(dto),
	        dataType: 'json',
	        success: function (data) {
	            if(data.code == 0){
	               //刷新父页面
		 			parent.location.reload();
	            }
	        }
    	});
    	return false;
 	})
	
})

//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}

	/*
	 *得到公寓楼列表
	 */
	function getApartment(){
		var dto = {};
	      $.ajax({
	      	    type: "POST",
	      	    dataType: "json",
	      	    url : API_URL_HEAD+"apartmentInfo/select",
	      	    contentType: "application/json; charset=utf-8",
	      	    async:false,
	      	    data:JSON.stringify(dto),
	      	  	success: function (data) {  
	      	  		if(data.code == "0"){
		      	  		var jsArr = data.data;
		      	  		var apartSnObj = $(".apartSn");
		      	  		createSelectOptions(apartSnObj,jsArr,0);
	      	  		}
	      	    },
	      	    error: function(data) { 
	      	    	console.log(data);
	      	    	layer.alert("服务器忙，请稍后重试");
	      	    }
	      	});	
	}
	/*
	 *得到公寓子级列表
	 */
	function getChildrenDistrict(type,id){
		//父select
		var parentSelect = "";
		//子select
		var childrenSelect = "";
		var endUrl="";
		//选择公寓
		if(type==1){
			parentSelect = $(".apartSn");
			childrenSelect = $(".floorSn");
			var dto = 
			{
				"apartmentId": parentSelect.val()
			}
			endUrl = "apartmentFloor/select";
		}else{//选择公寓层数
			parentSelect = $(".floorSn");
			childrenSelect = $(".roomSn");
			var dto = 
			{
				"floorId": parentSelect.val()
			}
			endUrl = "apartmentRooms/select";
		}
	      $.ajax({
	      	    type: "POST",
	      	    dataType: "json",
	      	    url : API_URL_HEAD+endUrl,
	      	    contentType: "application/json; charset=utf-8",
	      	    data:JSON.stringify(dto),
	      	    async:false,
	      	  	success: function (data) {  
	      	  		if(data.code == 0){
	      	  			$(childrenSelect).find("option:not(:first)").remove();
	      	  			$(".roomSn").find("option:not(:first)").remove();
			      	  		if(data.data!=null && data.data!=""){
			      	  				createSelectOptions(childrenSelect,data.data,type);
			      	  		}
	      	  		}
	      	    },
	      	    error: function(data) { 
	      	    	layer.alert("服务器忙，请稍后重试");
	      	    }
	      	});	
	}
	//生成select options
	function createSelectOptions(selObj,dataArray,type,id){
		var name = "";
		if(type==0)
		{
			name = "apartmentName";
		}else
		if(type==1)
		{
			name="floorName";
		}else
		{
			name= "roomName";
		}
		for(var i in dataArray){
			var every = dataArray[i];
			if(id==undefined){
				$(selObj).append("<option value='"+every["id"]+"'>"+every[name]+"</option>");
			}else{
				if(every["id"]==id){
					$(selObj).append("<option value='"+every["id"]+"'  selected=\"selected\">"+every["apartmentName"]+"</option>");
				}else{
					$(selObj).append("<option value='"+every["id"]+"'>"+every["apartmentName"]+"</option>");
				}
			}
			
		}
	}